﻿using BW.IBLL;
using BW.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using Microsoft.AspNetCore.StaticFiles;

namespace BW.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoomController : ControllerBase
    {
        IBaseBLL bll;
        public RoomController(IBaseBLL bll)
        {
            this.bll = bll;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> Add(RoomNumber t)
        {
            return await bll.Add(t);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="totalcount">总条数</param>
        /// <param name="totalpage">总页数</param>
        /// <param name="pageindex">共几页</param>
        /// <param name="pagesize">共几条</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShow( int pageindex = 1, int pagesize = 2)
        {
            int totalcount = 0;
            int totalpage = 0;
            var list=bll.GetShow(out totalcount, out totalpage,pageindex,pagesize);
            return Ok(new
            {
                totalcount = totalcount,
                totalpage = totalpage,
                list = list
            });
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IQueryable<RoomType> roomTypes()
        {
            return bll.roomTypes();
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Exlsx(int pageindex = 1, int pagesize = 2)
        {
            byte[] data = null;
            string t = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            string f = ("xxx.xlsx");
            IWorkbook book=new XSSFWorkbook();
            ISheet sheet = book.CreateSheet();
            IRow row1 = sheet.CreateRow(0);
            row1.CreateCell(0).SetCellValue("房间号");
            row1.CreateCell(1).SetCellValue("房型");
            int totalcount = 0;
            int totalpage = 0;
            var list = bll.GetShow(out totalcount, out totalpage, pageindex, pagesize);
            int c = 0;
            foreach (var item in list)
            {
                IRow row = sheet.CreateRow(c);
                row.CreateCell(0).SetCellValue(item.Roomnum);
                row.CreateCell(1).SetCellValue(item.Roomid);
                c++;
            }
            using(MemoryStream ms =new MemoryStream())
            {
                book.Write(ms);
                data = ms.ToArray();
            }
            return File(data,t,f);
        }
        [HttpPost]
        public async Task<IActionResult> Index(IFormFile file)
        {
            MemoryStream ms=new MemoryStream();
            file.CopyTo(ms);
            ms.Position = 0;
            IWorkbook book = new XSSFWorkbook(ms);
            ISheet sheet = book.GetSheetAt(0);
            int count=sheet.LastRowNum;
            List<RoomNumber> list=new List<RoomNumber>();
            for (int i = 0; i <= count; i++)
            {
                IRow row = sheet.GetRow(i);
                string a = row.GetCell(0).ToString();
                string b = row.GetCell(1).ToString();
                RoomNumber room = new RoomNumber()
                {
                   Roomnum=int.Parse(a),
                   Roomid=int.Parse(b),

                };
                list.Add(room);
            }
            var m=await bll.RoomAdd(list);
            return Ok(m);

        }
    }
}
